package com.hfzy.ihk.web.freeswitchCall.biz;

import com.hfzy.ihk.common.util.string.StringUtil;
import com.hfzy.ihk.web.freeswitchCall.constant.RedisFields;
import com.hfzy.ihk.web.freeswitchCall.constant.RedisKey;
import com.hfzy.ihk.web.freeswitchCall.context.DisruptorInitBean;
import com.hfzy.ihk.web.freeswitchCall.support.disruptor.DisruptorSupport;
import com.hfzy.ihk.web.freeswitchCall.support.disruptor.base.DisruptorEvent;
import com.hfzy.ihk.web.freeswitchCall.vo.FreeswitchRecordFile;
import com.lmax.disruptor.RingBuffer;
import org.freeswitch.esl.client.transport.event.EslEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * freeswitch接收录音文件业务处理类 可以在此进入队列
 */
@Lazy
@Component("freeswitchRecordFileBiz")
public class FreeswitchRecordFileBiz extends DisruptorSupport<FreeswitchRecordFile,DisruptorEvent<FreeswitchRecordFile>> {

    Logger logger =LoggerFactory.getLogger(FreeswitchRecordFileBiz.class);


    @Autowired
    DisruptorInitBean disruptorInitBean;

    @Autowired
    RedisTemplate redisTemplate;

    //对外访问的url
    @Value("${freeswitch.web.recordFileUrl}")
    private String  recordFileUrl;

    //项目的路径
   /* @Value("${server.servlet.context-path}")
    private String  contextPath;*/

    @Value("${freeswitch.web.url}")
    private String serverUrl;

    /**
     * 处理状态消息
     * @param
     */

    public void dealCallState(EslEvent event){

        Map<String, String> eventHeaders = event.getEventHeaders();

        String sessionId=eventHeaders.get("Channel-Call-UUID");

        logger.info("录音停止,开始处理,sessionId:"+sessionId);

        try {

            FreeswitchRecordFile freeswitchRecordFile=new FreeswitchRecordFile();

            freeswitchRecordFile.setOut_id(sessionId);

            freeswitchRecordFile.setCall_id(eventHeaders.get("Core-UUID"));

            String createUserId = (String) redisTemplate.opsForHash().get(RedisKey.CALL_HISTORY_RPIX + sessionId, RedisFields.CREATE_USER_ID);

            if (!StringUtil.isEmpty(createUserId)){
                freeswitchRecordFile.setCreateUserId(createUserId);
            }

            String recordFilePath =(String)redisTemplate.opsForHash().get(RedisKey.CALL_HISTORY_RPIX + sessionId + "_temp", "recordFilePath");

            String whereFrom =(String)redisTemplate.opsForHash().get(RedisKey.CALL_HISTORY_RPIX + sessionId, RedisFields.CALL_HISTORY_WHERE_FROM);

            freeswitchRecordFile.setWhereFrom(whereFrom);

            if(StringUtil.isNotNull(recordFilePath)){
                freeswitchRecordFile.setRecord_file(serverUrl+recordFileUrl+recordFilePath);
                logger.info("freeswitchLog :  生成freeswitch录音文件url:"+serverUrl+recordFileUrl+recordFilePath);
            }

            putInRingBuffer(freeswitchRecordFile);

        }catch (Exception e){

            logger.info("------------->录音结束后,处理录音出错,sessionId:"+sessionId);

        }

    }

    @Override
    protected RingBuffer<DisruptorEvent<FreeswitchRecordFile>> useRingBuffer(FreeswitchRecordFile data) {

        RingBuffer ringBuffer = null;
        String useType = "freeswitch录音文件";
        ringBuffer = DisruptorInitBean.getRingBufferFreeswitchRecordFile();
        checkRingBufferUse(ringBuffer,useType);
        return ringBuffer;

    }
}
